//
//  LandViewController.m
//  UI_元旦豆瓣半完整版
//
//  Created by dllo on 16/1/1.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "LandViewController.h"
#import "RegisterViewController.h"
#import "MyDataBaseHandle.h"
#import "RegisterModel.h"


@interface LandViewController ()<UITextFieldDelegate>

@end

@implementation LandViewController

- (void)dealloc {
    Block_release(_block);
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"登陆";
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    self.navigationItem.leftBarButtonItem = item;
#if 1
    {
    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(50, 120, 70, 40)];
    name.backgroundColor = [UIColor clearColor];
    name.text = @"用户名: ";
    [self.view addSubview:name];
    [name release];
    
    UITextField *nameText = [[UITextField alloc] initWithFrame:CGRectMake(140, 120, 180, 40)];
    nameText.placeholder = @"请输入账户名";
    nameText.borderStyle = UITextBorderStyleRoundedRect;
    nameText.backgroundColor = [UIColor clearColor];
    [self.view addSubview:nameText];
    [nameText release];
    nameText.delegate = self;
    nameText.tag = 1000;
    
    UILabel *key = [[UILabel alloc] initWithFrame:CGRectMake(50, 200, 70, 40)];
    key.backgroundColor = [UIColor clearColor];
    key.text = @"密码: ";
    [self.view addSubview:key];
    [key release];
    
    UITextField *keyText = [[UITextField alloc] initWithFrame:CGRectMake(140, 200, 180, 40)];
    keyText.placeholder = @"请输入密码";
    keyText.borderStyle = UITextBorderStyleRoundedRect;
    keyText.backgroundColor = [UIColor clearColor];
    [self.view addSubview:keyText];
    [keyText release];
    keyText.delegate = self;
    keyText.tag = 1001;
    
    UIButton *landButton = [UIButton buttonWithType:UIButtonTypeCustom];
    landButton.backgroundColor = [UIColor colorWithRed:0.38 green:0.84 blue:0 alpha:0.80];
    [landButton setTitle:@"登陆" forState:UIControlStateNormal];
    [landButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    landButton.frame = CGRectMake(50, 280, 100, 40);
    [self.view addSubview:landButton];
    landButton.layer.cornerRadius = 10;
    landButton.showsTouchWhenHighlighted = YES;
    [landButton addTarget:self action:@selector(landButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelButton.backgroundColor = [UIColor colorWithRed:1 green:0.45 blue:0 alpha:0.80];
    [cancelButton setTitle:@"注册" forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    cancelButton.frame = CGRectMake(200, 280, 120, 40);
    [self.view addSubview:cancelButton];
    cancelButton.layer.cornerRadius = 10;
    cancelButton.showsTouchWhenHighlighted = YES;
    [cancelButton addTarget:self action:@selector(cancelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
#endif
}
#if 1

// 登陆
- (void)landButtonAction:(UIButton *)button {
    UITextField *nameText = [self.view viewWithTag:1000];
    UITextField *keyText = [self.view viewWithTag:1001];
    NSArray *arr = [[MyDataBaseHandle sharedDataBaseHandle] selectTableRegist];
    for (RegisterModel *model in arr) {
        NSLog(@"name = %@, key = %@", model.name, model.key);
        if (![model.name isEqual:nameText.text] && ![model.key isEqual:keyText.text]) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请输入正确用户名和密码🤔" message:nil preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
            [alert addAction:action];
            
            [self presentViewController:alert animated:YES completion:nil];
            //        NSLog(@"%@", nameText.text);
            
        } else {
            self.block(@"注销");
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
            
        }
    }

    
}
// 注册
- (void)cancelButtonAction:(UIButton *)button {
    RegisterViewController *reVC = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:reVC animated:YES];
    
    [reVC release];
}

#endif

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (void)itemAction:(UIBarButtonItem *)item {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
